<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/28 0028
 * Time: 10:57
 */

namespace app\api\controller;

use app\admin\model\Integralproducts;
use app\admin\model\Integralrecord;
use app\admin\model\Integralrule;
use app\common\BaseHelper;
use app\common\model\User;
use app\common\model\Usermemberhistory;
use think\Db;
use think\Exception;

/**
 * Class Integral
 * @package app\api\controller
 * 积分
 * 丁应清
 */
class Integral extends BaseController
{
    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 积分明细
     */
    public function getIntegralRecord()
    {
        $param = self::getHttpParam();
        if (empty($param->tenantId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'tenantId不能这空');
        } else if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId不能这空');
        } else {
            $tenantId = $param->tenantId;
            $userId = $param->userId;
            $integralRecord = Integralrecord::where(['tenantId' => $tenantId, 'userId' => $userId])->order('create_time desc')->select();
            if (empty($integralRecord)) {
                return show(config('status.EMPTY_STATUS'), self::NOT_DATA, '未查到积分明细');
            } else {
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $integralRecord);
            }
        }
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 积分规则
     */
    public function getIntegralRule()
    {
        $param = self::getHttpParam();
        if (empty($param->tenantId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'tenantId不能这空');
        } else {
            $tenantId = $param->tenantId;
            $integralRule = Integralrule::where('tenantId', $tenantId)->find();
            if (empty($integralRule)) {
                return show(config('status.EMPTY_STATUS'), self::NOT_DATA, '未查到积分规则');
            } else {
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $integralRule);
            }
        }
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 积分商品
     */
    public function getIntegralProduct()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;
        if (empty($param->storeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'storeId不能这空');
        } else {
            if (!empty($param->start)) {
                $start = $param->start;
            }
            if (!empty($param->length)) {
                $length = $param->length;
            }
            $storeId = $param->storeId;
            $where = [
                'storeId' => $storeId,
                'status' => ['neq', '2']
            ];
            $integralProducts = Integralproducts::where($where)->limit($start, $length)->select();
            if (empty($integralProducts)) {
                return show(config('status.EMPTY_STATUS'), self::NOT_DATA, '未查到积分商品');
            } else {
                for ($i = 0; $i < count($integralProducts); $i++) {
                    $product = \app\admin\model\Product::where('id', $integralProducts[$i]->productId)->find();
                    $integralProducts[$i]['product'] = $product;
                }
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $integralProducts);
            }
        }
    }

    //会员消费送积分
    public function XiaofeiSongjiFen($order){
        Db::startTrans();
        try{

            $user = User::where(['id'=>$order->createUser,'isMember'=>2])->where('memberendtime',['>',time()],['=',0],'OR')->find();
            if($user){
                $Usermemberhistory = Usermemberhistory::where(['userid'=>$order->createUser])->order('create_time desc')->find();
                if($Usermemberhistory->consumeMoney && $Usermemberhistory->consumeIntegral){
                    if($order->total >= $Usermemberhistory->consumeMoney){
                        //为会员增加积分
                        $user->setInc('integral',$Usermemberhistory->consumeIntegral);
                        //增加积分明细记录
                        $this->insertRecord($order,\app\common\model\Integralrecord::TYPE_2,$Usermemberhistory->consumeIntegral);

                        //发送模板消息 TODO
                    }
                }
            }
            Db::commit();
        }catch (Exception $ex){
            Db::rollback();
        }


    }

    //写入积分记录
    public function insertRecord($order,$type,$integral){
        $Integralrecord = new \app\common\model\Integralrecord();
        $Integralrecord->id = BaseHelper::getUUID();
        $Integralrecord->type = $type;
        $Integralrecord->orderId = $order->id;
        $Integralrecord->integral = $integral;
        $Integralrecord->orderPrice = $order->total;
        $Integralrecord->userId = $order->createUser;
        $Integralrecord->tenantId = $order->tenantId;
        $Integralrecord->save();
    }
    public function insertRecordCz($Useraccountdetails,$type,$integral){
        $Integralrecord = new \app\common\model\Integralrecord();
        $Integralrecord->id = BaseHelper::getUUID();
        $Integralrecord->type = $type;
        $Integralrecord->orderId = $Useraccountdetails->id;
        $Integralrecord->integral = $integral;
        $Integralrecord->orderPrice = $Useraccountdetails->money;
        $Integralrecord->userId = $Useraccountdetails->userId;
        $Integralrecord->tenantId = $Useraccountdetails->tenantId;
        $Integralrecord->save();

    }
}